﻿using Hidistro.UI.Common.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Juse.Interface;
using JSShop.Model;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Hidistro.Membership.Context;
using wnxd.javascript;
using System.Web;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_ProductBundling : AscxTemplatedWebControl
    {
        private Repeater PList;
        private Literal total;
        private Literal bprice;
        private Literal subprice;
        private HtmlAnchor buy;
        private int productId;
        protected override void AttachChildControls()
        {
            this.PList = (Repeater)this.FindControl("PList");
            this.total = (Literal)this.FindControl("total");
            this.bprice = (Literal)this.FindControl("bprice");
            this.subprice = (Literal)this.FindControl("subprice");
            this.buy = (HtmlAnchor)this.FindControl("buy");
            if (int.TryParse(this.Page.Request.QueryString["productId"], out this.productId))
            {
                ProductBrowser ProductBrowser = new ProductBrowser();
                List<BundlingInfo> list = ProductBrowser.GetBundlingList(this.productId, 1);
                if (list != null && list.Count > 0)
                {
                    BundlingInfo info = list[0];
                    aspnet_Members member = null;
                    Member m = HiContext.Current.User as Member;
                    if (m != null) member = new json(m).TryConvert(typeof(aspnet_Members)) as aspnet_Members;
                    IList<int> pids = info.BundlingItemInfos.Select(model => model.ProductID).ToList();
                    IList<vw_Hishop_BrowseProductList> vwlist = ProductBrowser.GetVistiedProducts(member, pids);
                    this.PList.DataSource = vwlist;
                    this.PList.DataBind();
                    decimal price = 0;
                    if (vwlist != null)
                    {
                        foreach (vw_Hishop_BrowseProductList item in vwlist)
                        {
                            price += item.SalePrice;
                        }
                    }
                    this.total.Text = price.ToString("F2");
                    this.bprice.Text = info.Price.ToString("F2");
                    this.subprice.Text = (price - info.Price).ToString("F2");
                    this.buy.HRef = "http://order.x-juse.com/SubmmitOrder.aspx?buyAmount=1&Bundlingid=" + info.BundlingID + "&from=Bundling&sdomain=" + System.Web.HttpUtility.UrlEncode("http://" + this.Page.Request.Url.Authority);
                    return;
                }
            }
            this.Visible = false;
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/Common_Comment/wnxd_ProductBundling.ascx";
            }
            base.OnInit(e);
        }
    }
}
